<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing: Cache for apps
 *
 * @category   	Joomzf Cms
 * @package    	Joomzf cache
 * @copyright  	Copyright vinanewstar.vn@gmail.com
 * @author		GiangNam (vinanewstar.vn@gmail.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 May 27, 2012 - 5:03:32 PM
 */
class Joomzf_Cache{
	/**
	 * 
	 * @param array $options
	 * @return Ambigous <Zend_Cache_Core, Zend_Cache_Frontend, mixed>
	 */
	public static function init(array $options = null){
		if($options == null){
			$options['frontend'] = 'Core';
			$options['backend'] = 'File';
			$options['frontendOptions'] = array('cat_id_prefix'=>'core_',
												'lifetime'=>$time_out,
												'automatic_serialization' => true );
			$options['backendOptions'] = array('cache_dir'=>CACHE_PATH);
		} else{
			if ($options['frontend'] = null){
				$frontend = 'Core';
			}
			if ($options['backend'] = null){
				$backend = 'File';
			}
			if($options['frontendOptions'] == null){
				$frontendOptions = array('cat_id_prefix'=>'joomzf_',
						'lifetime'=>$time_out,
						'automatic_serialization' => true );
			}
			if($options['frontendOptions']['cat_id_prefix'] == null){
				$options['frontendOptions']['cat_id_prefix'] = 'joomzf_';
			}
			if($options['backendOptions'] == null){
				$backendOptions = array('cache_dir'=>CACHE_PATH);
			}
		}
		$frontend = $options['frontend'];
		$backend = $options['backend'];
		$frontendOptions = $options['frontendOptions'];
		$backendOptions = $options['backendOptions'];
		return   Zend_Cache::factory($frontend, $backend,
				$frontendOptions , $backendOptions);
	}
	public static function initLanguage(){
		$time_out = CACHE_LANG;
		$frontend = 'Core';
		$backend = 'File';
		$frontendOptions = array('cat_id_prefix'=>'jzflang_',
									'lifetime'=>$time_out,
									'automatic_serialization' => true ); 
		$backendOptions = array('cache_dir'=>CACHE_PATH.'/languages');
		return   Zend_Cache::factory($frontend, $backend,
				$frontendOptions , $backendOptions);
	}
	public static function initModule($time_out){
		if($time_out == null) $time_out = CACHE_MODULE;
		$frontend = 'Core';
		$backend = 'File';
		$frontendOptions = array('cat_id_prefix'=>'jzfmod_',
									'lifetime'=>$time_out,
									'automatic_serialization' => true ); 
		$backendOptions = array('cache_dir'=>CACHE_PATH.'/modules');
		return   Zend_Cache::factory($frontend, $backend,
				$frontendOptions , $backendOptions);
	}
	public static function getVariableFromCache($cache,$catche_id = null,$currentObj = null,$callBack = null,$callBackParams = null){
		$result = $cache->load($catche_id);
		if( $result == false){
			$result = $currentObj->$callBack($callBackParams);
			$cache->save($result,$catche_id);
		}
		return $result;
	}
}